package LeetCode._01_树;

import java.util.PriorityQueue;

public class _703_数据流中的第K大元素 {
    class KthLargest {

        private int k;
        private PriorityQueue<Integer> pq = new PriorityQueue<>(); //小顶堆
        public KthLargest(int k, int[] nums) {
            //保留前K大的元素
            for (int num : nums) {
                pq.offer(num);
                if(pq.size()>k){
                    pq.poll();
                }
            }
            this.k = k;
        }

        public int add(int val) {
            pq.offer(val);
            if(pq.size()>k){
                pq.poll();
            }
            return pq.peek();
        }
    }
}
